<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('机构/个人')" prop="organOrPersonal">
            <el-select v-model="form.organOrPersonal" value="" clearable :placeholder="$t('机构/个人')">
              <el-option
                v-for="dict in organOrPersonalOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--联系人-->
      <div class="area-header">
        <span class="area-header-title">{{ $t('联系人') }}</span>
      </div>
      <el-table :data="form.listedContactsList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('联系人')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('联系人') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'listedContactsList.' + scope.$index + '.contacts'" :rules="listedContactsListRules.contacts">
              <el-input v-model="scope.row.contacts" :placeholder="$t('联系人')" show-word-limit maxlength="15" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('联系电话')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'listedContactsList.' + scope.$index + '.contactNumber'" :rules="listedContactsListRules.contactNumber">
              <el-input v-model="scope.row.contactNumber" :placeholder="$t('联系电话')" show-word-limit maxlength="11" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('联系地址')">
          <template slot-scope="scope">
            <el-form-item :prop="'listedContactsList.' + scope.$index + '.contactAddress'">
              <el-input v-model="scope.row.contactAddress" :placeholder="$t('联系地址')" show-word-limit maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column :label="$t('备注')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'listedContactsList.' + scope.$index + '.remark'">
              <el-input v-model="scope.row.remark" type="textarea" maxlength="2000" :autosize="{ minRows:1,maxRows:10 }" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
          <template slot-scope="scope">
            <el-button v-hasPermi="['listed:contacts:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addListedContacts">{{ $t('添加联系人') }}</el-button>
      </div>

      <!--历史交易明细-->
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('历史交易明细') }}</span>
        </div>
        <el-table v-loading="loading" :data="listedShareSubDetailList" border>
          <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
            <template slot-scope="scope">
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('证券账号')" align="center" prop="securityAccount" min-width="180" show-overflow-tooltip />
          <el-table-column :label="$t('证券公司')" align="center" prop="securityName" min-width="180" show-overflow-tooltip />
          <el-table-column :label="$t('减持日期')" align="center" prop="subDate" min-width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.subDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('实际减持总金额') + $t('金额单位')" align="center" prop="totalSubAmount" min-width="180">
            <template slot-scope="scope">
              <span>{{ moneyFormat(scope.row.totalSubAmount, 'w') }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('实际减持总股数(万股)')" align="center" prop="totalSubShare" min-width="180">
            <template slot-scope="scope">
              <span>{{ moneyFormat(scope.row.totalSubShare) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('减持比例(%)')" align="center" prop="subShareRate" min-width="180">
            <template slot-scope="scope">
              <span>{{ moneyFormat(scope.row.subShareRate) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('交易方式')" align="center" prop="tradingWay" min-width="180" :formatter="tradingWayFormat" show-overflow-tooltip />
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getListedShareSubDetailList"
        />
      </div>
      <FileTable :list.sync="form.fileList" data-type="listed_business_opponent" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="listed:businessOpponent" />
    </div>
  </div>
</template>

<script>
    import { saveListedBusinessOpponent, getListedBusinessOpponent } from '@/api/deal/listed/businessOpponent/listedBusinessOpponent'
    import { delListedContacts } from '@/api/deal/listed/contacts/listedContacts'
    import { listListedSecurity } from '@/api/deal/listed/security/listedSecurity'
    import { listShareSubDetail } from '@/api/deal/listed/shareSub/listedShareSubDetail'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  fileList: [],
                  listedContactsList: [],
                  detailList: []
                },
                // 遮罩层
                loading: false,
                // 机构/个人类型
                organOrPersonalOptions: [],
                // 交易方式
                tradingWayOptions: [],
                // 历史交易明细
                listedShareSubDetailList: [],
                // 是否隐藏其它按钮
                otherBtnHide: false,
                // 总条数
                total: 0,
                // 证券账号options
                securityOptions: [],
                // 历史交易明细
                queryParams: {
                  opponentId: '',
                  pageNum: 1,
                  pageSize: 10
                },
                // 表单校验
                rules: {
                  name: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  organOrPersonal: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
                },
                listedContactsListRules: {
                  contacts: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  contactNumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: this.$t('请输入正确的手机号码'),
                      trigger: 'blur'
                    }
                  ]
                }
            }
        },
        created() {
          // 机构/个人类型
          this.getDicts('lp_type').then(response => {
            this.organOrPersonalOptions = response.data
          })
          // 交易方式
          this.getDicts('listed_trading_way').then(response => {
            this.tradingWayOptions = response.data
          })
          // 获取证券账户options
          listListedSecurity({}).then(response => {
            this.securityOptions = response.rows
          })
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getListedBusinessOpponent(data.id).then(response => {
                        this.form = response.data
                        this.queryParams.opponentId = response.data.id
                        this.getListedShareSubDetailList()
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            /** 历史交易明细 */
            getListedShareSubDetailList() {
              this.loading = true
              listShareSubDetail(this.queryParams).then(response => {
                this.listedShareSubDetailList = response.rows
                this.total = response.total
                this.loading = false
              })
            },
            // 交易方式
            tradingWayFormat(row) {
              return this.selectDictLabel(this.tradingWayOptions, row.tradingWay)
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    name: undefined,
                    dealType: (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType),
                    organOrPersonal: undefined,
                    listedContactsList: [],
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: [],
                    detailList: []
                }
                this.resetForm('form')
            },
            // 联系人
            addListedContacts() {
              const row = {
                id: undefined,
                contacts: '',
                contactNumber: '',
                contactAddress: '',
                remark: ''
              }
              if (!(this.form.listedContactsList instanceof Array)) {
                this.$set(this.form, 'listedContactsList', [])
              }
              this.$nextTick(() => {
                this.form.listedContactsList.push(row)
              })
            },
            // 删除联系人
            delHandle(index, row) {
              // eslint-disable-next-line no-undef
              const id = row.id
              if (id !== undefined && id !== '' && id != null) {
                this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
                  confirmButtonText: this.$t('确定'),
                  cancelButtonText: this.$t('取消'),
                  type: 'warning'
                })
                  .then(function() {
                    return delListedContacts(id)
                  }).then(() => {
                  this.form.listedContactsList.splice(index, 1)
                  return this.msgSuccess(this.$t('删除成功'))
                })
                  .catch(function() {})
              } else {
                this.form.listedContactsList.splice(index, 1)
              }
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveListedBusinessOpponent(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           }

        }

    }
</script>
